<?php
session_start();
global $idMenuMain, $prefix_url;
$idMenuMain = 121;
$prefix_url = "/";
global $lg;

include('./includes/config.php');

if(isset($_GET['lg'])){
	$lg = mysql_real_escape_string($_GET["lg"]);
	$lg = strip_tags($lg);
	$_SESSION['lg'] = mysql_real_escape_string($_GET["lg"]);
}
else{
	$lg = "vn";
	$_SESSION['lg'] = "vn";		
}

// begin cache
	global $cache_filename;
	$cache_time = 0;//24*60*60;
	if(isset($_SESSION["store_login"])){
		$cache_time = 0;
	}
	//$cache_folder = 'cache/';
	//$cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']) . "_" . $_SESSION['lg'] . ".txt"; 
	//$cache_created  = (file_exists($cache_filename)) ? filemtime($cache_filename) : 0;  
	
	//if (file_exists($cache_filename) && ((time() - $cache_created) < $cache_time)) {  
	//	include($cache_filename);
	//}
	//else{
		
	//ob_start();
	
	// end cache
?>
<?php

if($lg=="en")
	$prefix_url = "/en/";

	
include('language/'.$_SESSION['lg'].'.php');

define('TPL_DIR','templates/site/');
define('SRC_DIR','sources/site/');
define('CLASS_DIR','class/');

include("./includes/constant.php");
include("./includes/functions.php");
include("./includes/va_db.php");

include(CLASS_DIR.'bao_tri.class.php');
include(CLASS_DIR."info.class.php");
include(CLASS_DIR."yahoo.class.php");
include(CLASS_DIR."skype.class.php");
include(CLASS_DIR."template.php");
include(CLASS_DIR."cart.class.php");

if(BaoTri::Check()){
	include(TPL_DIR.'baotri.php');
}
else{
	global $page,$do,$act,$tpl, $db, $title_page, $keywords, $descriptions;
	//echo '[do:'.$do.']' . ' [act:'.$act.']'.'[cat1:'.$_GET['cat1'].']'.'[cat2:'.$_GET['cat2'].']'.'[unique_key:'.$_GET['unique_key'].']';
	if(isset($_GET['do'])){
		//echo 'co do';
		$do   = CleanSQLInjection(isset($_GET["do"])  ? $_GET["do"] :'main');
		$act   = CleanSQLInjection(isset($_GET["act"])  ? $_GET["act"]  : "main");
		
		$sql = "select * from categories where unique_key_$lg='index' and (pid=121 OR pid=598)";
		$cat1 = $db->getRow($sql);
				
		//seo
		$title_page = $cat1["title_$lg"];
		$keywords = $cat1["keyword_$lg"];
		$descriptions = $cat1["des_$lg"];
	}
	else{
		//echo 'khong co do';
		
		if(isset($_GET['cat1'])){
			$cat1_key = CleanSQLInjection($_GET['cat1']);
			//echo '<br/>[cat1_key:'.$cat1_key.']';
			if($cat1_key == "index"){
				$do = "main";
				$act = "main";
				$sql = "select * from categories where unique_key_$lg='$cat1_key' and (pid=121 OR pid=598)";
				$cat1 = $db->getRow($sql);
				//echo 'test cat1_key == index';
				//seo
				$title_page = $cat1["title_$lg"];
				$keywords = $cat1["keyword_$lg"];
				$descriptions = $cat1["des_$lg"];
			}
			else{
				$sql = "select * from categories where unique_key_$lg='$cat1_key' and (pid=121)";
				
				$cat1 = $db->getRow($sql);
				//echo '<br/>'.'[cat1_key != index:'.print_r($cat1).']';
				//seo
				$title_page = $cat1["title_$lg"];
				$keywords = $cat1["keyword_$lg"];
				$descriptions = $cat1["des_$lg"];
				
				if($cat1['has_child'] == 1){
					$do = "submenu";
					$act = "list";
					//echo '<br/>co con<br/>';
				}
				else{
					$sql = "select do, act from component where id=".$cat1['comp'];
					//echo $sql;
					$r = $db->getRow($sql);
					$do = $r['do'];
					$act = $r['act'];
				}
				if(isset($_GET['cat2'])){
					global $cat2;
					$cat2_key = CleanSQLInjection($_GET['cat2']);
					$sql = "select * from categories where unique_key_$lg='$cat2_key' and pid=" . $cat1['id'];
					
					$cat2 = $db->getRow($sql);
					//echo '[cat2:'.$cat2.']';

					//seo
					$title_page = $cat2["title_$lg"];
					$keywords = $cat2["keyword_$lg"];
					$descriptions = $cat2["des_$lg"];
				
					$sql = "select do, act from component where id=".$cat2['comp'];
					$r = $db->getRow($sql);
					$do = $r['do'];
					$act = $r['act'];
					
					if($cat2['has_child'] == 1){
					$do = "submenu";
					$act = "list";
					}
					else{
						$sql = "select do, act from component where id=".$cat2['comp'];
						$r = $db->getRow($sql);
						$do = $r['do'];
						$act = $r['act'];
					}
					
					if(isset($_GET['cat3'])){
						global $cat3;
						$cat3_key = CleanSQLInjection($_GET['cat3']);
						$sql = "select * from categories where unique_key_$lg='$cat3_key' and pid=" . $cat2['id'];
						$cat3 = $db->getRow($sql);
	
						//seo
						$title_page = $cat3["title_$lg"];
						$keywords = $cat3["keyword_$lg"];
						$descriptions = $cat3["des_$lg"];
					
						$sql = "select do, act from component where id=".$cat3['comp'];
						$r = $db->getRow($sql);
						$do = $r['do'];
						$act = $r['act'];
					}
				}
				if(isset($_GET['unique_key'])){
					$act = "detail";
				}
			}
		}
		else{
			$sql = "select * from categories where unique_key_$lg='index' and (pid=121 OR pid=598)";
			$cat1 = $db->getRow($sql);
			//echo $sql;
			//echo '<br/>khong co do + khong co cat1';
			//seo
			$title_page = isset($cat1["title_$lg"])?$cat1["title_$lg"]:'Happy Shop';
			
			$keywords = $cat1["keyword_$lg"];
			$descriptions = $cat1["des_$lg"];
			$do = "main";
			$act = "main";
		}
		
	}
	//echo '<br/>[do:'.$do.']' . ' [act:'.$act.']'.'[cat1:'.$_GET['cat1'].']'.'[cat2:'.$_GET['cat2'].']'.'[unique_key:'.$_GET['unique_key'].']';
	$page = isset($_GET["page"])?is_numeric($_GET["page"])?$_GET["page"]:'1':'1';
	
	$file_index = "./kam/index.php";
	//echo $file_index;
	if (file_exists($file_index)){
		require($file_index);
	}
	else{
		$msg = REDIRECT_MESSAGE;
		$p = '/index.php';
	}	
	//show
	//$filename2 = './kam/modules/site/'.$do.'.php';
	//include($filename2);
}
?>
<?php  
	//global $cache_filename;
	//file_put_contents($cache_filename, ob_get_contents());  
	//ob_end_flush();  
	//}
?>
